package com.stylefeng.guns.modular.customer.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.Permission;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.customer.dao.AreaDao;
import com.stylefeng.guns.modular.customer.service.IAreaService;
import com.stylefeng.guns.modular.customer.warpper.AreaWrapper;
import com.stylefeng.guns.modular.system.model.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 区域管理控制器
 *
 * @author fengshuonan
 * @Date 2018-04-09 16:53:57
 */
@Controller
@RequestMapping("/area")
public class AreaController extends BaseController {

    private String PREFIX = "/customer/area/";

    @Autowired
    private IAreaService areaService;
    @Autowired
    private AreaDao areaDao;

    /**
     * 跳转到区域管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "area.html";
    }

    /**
     * 跳转到添加区域管理
     */
    @RequestMapping("/area_add")
    public String areaAdd() {
        return PREFIX + "area_add.html";
    }

    /**
     * 跳转到修改区域管理
     */
    @RequestMapping("/area_update/{areaId}")
    public String areaUpdate(@PathVariable Integer areaId, Model model) {
        Area area = areaService.selectById(areaId);
        Map<String,Object> map = BeanKit.beanToMap(area);
        model.addAttribute("item",new AreaWrapper(map).warp());
        LogObjectHolder.me().set(area);
        return PREFIX + "area_edit.html";
    }

    /**
     * 获取区域管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.areaDao.list(condition);
        return super.warpObject(new AreaWrapper(list));
    }

    /**
     * 获取的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.areaDao.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }


    /**
     * 新增区域管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission
    public Object add(Area area) {
        areaSetPids(area);
        return this.areaService.insert(area);
    }

    /**
     * 删除区域管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission(Const.ADMIN_NAME)
    public Object delete(@RequestParam Integer areaId) {
        areaService.deleteById(areaId);
        return SUCCESS_TIP;
    }

    /**
     * 修改区域管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission
    public Object update(Area area) {
        areaSetPids(area);
        areaService.updateById(area);
        return SUCCESS_TIP;
    }

    /**
     * 区域管理详情
     */
    @RequestMapping(value = "/detail/{areaId}")
    @ResponseBody
    public Object detail(@PathVariable("areaId") Integer areaId) {
        return areaService.selectById(areaId);
    }


    private void areaSetPids(Area area) {
        if (ToolUtil.isEmpty(area.getPid()) || area.getPid().equals(0)) {
            area.setPid(0);
            area.setPids("[0],");
        } else {
            int pid = area.getPid();
            Area area1 =areaService.selectById(pid);
            String pids = area1.getPids();
            area.setPid(pid);
            area.setPids(pids + "[" + pid + "],");
        }
    }

}
